import Vue from 'vue';
import Router from 'vue-router';
import store from '../store/store';
const login = resolve => require(['../pages/login.vue'], resolve);
const register = resolve => require(['../pages/register.vue'], resolve);
const forgetLoginPws = resolve => require(['../pages/forgetLoginPws.vue'], resolve);
const machinemarket = resolve => require(['../pages/machineMarket.vue'], resolve);
const classicMachineDetail = resolve => require(['../pages/classicMachineDetail.vue'], resolve);
const fastMachineDetail = resolve => require(['../pages/fastMachineDetail.vue'], resolve);
const classicMachinePay = resolve => require(['../pages/classicMachinePay.vue'], resolve);
const fastMachinePay = resolve => require(['../pages/fastMachinePay.vue'], resolve);
const paymentSuccessful = resolve => require(['../pages/paymentSuccessful.vue'], resolve);
const asset = resolve => require(['../pages/asset.vue'], resolve);
const recharge = resolve => require(['../pages/recharge.vue'], resolve);
const rechargePublic = resolve => require(['../pages/rechargePublic.vue'], resolve);
const rechargeResult = resolve => require(['../pages/rechargeResult.vue'], resolve);
const assetCanUse = resolve => require(['../pages/assetCanUse.vue'], resolve);
const assetCoins = resolve => require(['../pages/assetCoins.vue'], resolve);
const bill = resolve => require(['../pages/bill.vue'], resolve);
const cashWithdraw = resolve => require(['../pages/cashWithdraw.vue'], resolve);
const cashwithdrawResult = resolve => require(['../pages/cashwithdrawResult.vue'], resolve);
const outCoins = resolve => require(['../pages/outCoins.vue'], resolve);
const addressManage = resolve => require(['../pages/addressManage.vue'], resolve);
const contractList = resolve => require(['../pages/contractList.vue'], resolve);
const renewalOrder = resolve => require(['../pages/renewalOrder.vue'], resolve);
const transferOrder = resolve => require(['../pages/transferOrder.vue'], resolve);
const returnOrder = resolve => require(['../pages/returnOrder.vue'], resolve);
const profitList = resolve => require(['../pages/profitList.vue'], resolve);
const transferList = resolve => require(['../pages/transferList.vue'], resolve);
const rechargeElectric = resolve => require(['../pages/rechargeElectric.vue'], resolve);
const safeCenter = resolve => require(['../pages/safeCenter.vue'], resolve);
const relNameVerification = resolve => require(['../pages/relNameVerification.vue'], resolve);
const verificationResult = resolve => require(['../pages/verificationResult.vue'], resolve);
const bankCardManage = resolve => require(['../pages/bankCardManage.vue'], resolve);
const addBank = resolve => require(['../pages/addBank.vue'], resolve);
const bhpayInfo = resolve => require(['../pages/bhpayInfo.vue'], resolve);
const outCoinAddressManager = resolve => require(['../pages/outCoinAddressManager.vue'], resolve);
const invited = resolve => require(['../pages/invited.vue'], resolve);
const powerLevel = resolve => require(['../pages/powerLevel.vue'], resolve);

let mainPages = [
  {path: '/', redirect : '/machinemarket'},
  {path: '/login', component : login, meta : {title:'登录'}},
  {path: '/register', component : register, meta : {title:'注册'}},
  {path: '/forgetLoginPws', component : forgetLoginPws, meta : {title:'忘记密码'}},
  {path: '/machinemarket', component : machinemarket, meta : {title:'算力市场'}},
  {path: '/classicMachineDetail', component : classicMachineDetail, meta : {title:'合约详情'}},
  {path: '/fastMachineDetail', component : fastMachineDetail, meta : {title:'合约详情'}},
  {path: '/classicMachinePay', component : classicMachinePay, meta : {title:'订单总览'}},
  {path: '/paymentSuccessful', component : paymentSuccessful, meta : {title:'下单结果'}},
  {path: '/fastMachinePay', component : fastMachinePay, meta : {title:'订单总览'}},
]; //主页面
let myCenter = [
  {path: '/asset', component : asset, meta : {title:'我的资产'}},
  {path: '/recharge', component : recharge, meta : {title:'充值'}},
  {path: '/rechargePublic', component : rechargePublic, meta : {title:'对公转账'}},
  {path: '/rechargeResult', component : rechargeResult, meta : {title:'充值结果'}},
  {path: '/assetCanUse', component : assetCanUse, meta : {title:'可用资产'}},
  {path: '/assetCoins', component : assetCoins, meta : {title:'数字货币'}},
  {path: '/bill', component : bill, meta : {title:'资产账单'}},
  {path: '/cashWithdraw', component : cashWithdraw, meta : {title:'提现'}},
  {path: '/cashwithdrawResult', component : cashwithdrawResult, meta : {title:'申请提现成功'}},
  {path: '/outCoins', component : outCoins, meta : {title:'提币'}},
  {path: '/addressManage', component : addressManage, meta : {title:'提币地址管理'}},
  {path: '/contractList', component : contractList, meta : {title:'合约列表'}},
  {path: '/renewalOrder', component : renewalOrder, meta : {title:'续单'}},
  {path: '/transferOrder', component : transferOrder, meta : {title:'转让'}},
  {path: '/returnOrder', component : returnOrder, meta : {title:'退单'}},
  {path: '/profitList', component : profitList, meta : {title:'算力产出'}},
  {path: '/transferList', component : transferList, meta : {title:'算力转让'}},
  {path: '/rechargeElectric', component : rechargeElectric, meta : {title:'充值电费包'}},
  {path: '/safeCenter', component : safeCenter, meta : {title:'账户安全'}},
  {path: '/relNameVerification', component : relNameVerification, meta : {title:'实名校验'}},
  {path: '/verificationResult', component : verificationResult, meta : {title:'实名校验'}},
  {path: '/bankCardManage', component : bankCardManage, meta : {title:'银行卡管理'}},
  {path: '/addBank', component : addBank, meta : {title:'添加银行卡'}},
  {path: '/bhpayInfo', component : bhpayInfo, meta : {title:'BHpay授权'}},
  {path: '/outCoinAddressManager', component : outCoinAddressManager, meta : {title:'提币地址管理'}},
  {path: '/powerLevel', component : powerLevel, meta : {title:'算领'}},
  {path: '/invited', component : invited, meta : {title:'邀请好友'}},
];//我的个人中心页面

Vue.use(Router);
const router = new Router({
  // mode:"mode",
  routes : mainPages, //目前只展示主页面，不展示个人中心页面
  // routes : [ mainPages , myCenter].flat(10), //需要展示个人中心页面，打开此行，屏蔽上一行
});

export default router;

if (localStorage.token) {
  store.commit('token', localStorage.token)
}
if (localStorage.header) {
  store.commit('header', localStorage.header)
}
if (localStorage.uid) {
  store.commit('uid' , localStorage.uid)
}
if (localStorage.mobile) {
  store.commit('mobile' , localStorage.mobile)
}
if (localStorage.name) {
  store.commit('name' , localStorage.name)
}
// store.commit('storageRemove','all');
const acceptViews = [
  '/asset','/login' , '/register', '/forgetLoginPws' , '/machinemarket' ,'/classicMachineDetail' , '/fastMachineDetail',
  '/fastMachinePay' , '/classicMachinePay' , "/asset",
  /*------*/
  '/invited' , '/powerLevel'
];
//未登录/后端信息验证不正确，允许访问的页面，其他页面不允许访问

const needStoreChoseCoupons = ['/need_electric_power_pay' , '/no_electric_power_pay' , '/chose_coupon' ];
//选择的优惠券，需要保存store的页面，其他页面删除

const marketActive = ['/machinemarket' , '/classicMachineDetail' , '/classicMachinePay' , '/fastMachineDetail' ,'/fastMachinePay'];
//设置头部市场选中

router.beforeEach( function( to , from , next ) {
  if (to.meta.title) {
    document.title = '人人矿场 | ' + to.meta.title;
  }
  let rrm_header = document.querySelector('#rrm_header');
  if(marketActive.indexOf(to.path) != -1){
    if( rrm_header ){
      let a = rrm_header.querySelectorAll('a');
      for( let i = 0 ; i < a.length ;i++){
        a[i].classList.remove('active');
      }
      let marketLink = a[1];
      if( !marketLink.classList.contains('active')){
        marketLink.classList.add('active');
      }
    }
  }
  else{
    if( rrm_header ){
      let a = rrm_header.querySelectorAll('a');
      for( let i = 0 ; i < a.length ;i++){
        a[i].classList.remove('active');
      }
      a = null;
    }
  }
  if(needStoreChoseCoupons.indexOf(to.path) === -1 ){
    store.commit('chose_coupons' , []); //清除优惠券选择
    store.commit('rent_num' , ''); //清除租用数量
    store.commit('buy_math_power' , false); //清除购买算力宝
    store.commit('payType' , ''); //清除支付方式
  }
  // if(store.state.uid && to.path === '/index'){
  //     next ({path:'/entrance',replace: true}); //存在登录信息，index替换为entrance
  //     return;
  // }
  // console.log('store.state.mobile',store.state.mobile);
  if( to.matched.length === 0 ){
    //进入不存在路由，默认回上一页，上一页不存在则回个人主页
    from.path ? next(from.path) : next ({path:'/login',replace: true});
  } else if ( !store.state.uid ) {
      if( acceptViews.indexOf(to.path) === -1 ){
        require('../common/js/toast').toast('请先登录');
        next ('/login'); //验证信息不正确，默认进入登录页面
      } else{
        next();
      }
  } else {
    next();
  }
});

router.afterEach( function() {
  window.scrollTo(0, 0);
  let body = document.querySelector('body');
  body.scroll(0,0);
  body = null;
});
